package com.panopset.swing;

import static com.panopset.Util.centerInScreen;
import static com.panopset.Util.parseInt;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import com.panopset.gui.LookAndFeel;

/**
 * Your basic Swing application frame.
 *
 * @author Karl Dinwiddie
 */
public class SimpleFrame {

    /**
     * Get application property.
     *
     * @param key
     *            Key.
     * @return Property, empty String if not found.
     */
    public final String get(final String key) {
        return App.app.s.get(key);
    }

    /**
     * Put application property.
     *
     * @param key
     *            Key.
     * @param value
     *            Value.
     */
    public final void put(final String key, final String value) {
    	App.app.s.put(key, value);
    }

    public static Dimension DEFAULT_SIZE 
        = LookAndFeel.Dimensions.WINDOW.getValue();
    /**
     * Show application main frame.
     */
    final void show() {
        Integer w = parseInt(get(W));
        Integer h = parseInt(get(H));
        Integer x = parseInt(get(X));
        Integer y = parseInt(get(Y));
        Dimension size = null;
        Point location = null;
        if (w > 0 && h > 0) {
            size = new Dimension(w, h);
            location = new Point(x, y);
        } else {
            size = DEFAULT_SIZE;
            location = centerInScreen(size);
        }
        getFrame().setSize(size);
        getFrame().setLocation(location);
        getFrame().setVisible(true);
    }

    /**
     * Frame.
     */
    private JFrame f;

    /**
     * @return Frame.
     */
    public JFrame getFrame() {
        if (f == null) {
            f = new JFrame();
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.addWindowListener(new WindowAdapter() {
                public void windowClosing(final WindowEvent e) {
                    App.app.quit();
                }
            });
            if (App.app.icon != null) {
                f.setIconImage(App.app.icon.getImageResource().getImage());
            }
            f.setLayout(new GridLayout(1, 1));
            f.setTitle(App.app.getApplicationName());
            f.add(App.app.createMainPanel());
            App.app.s.loadGui();
            f.setJMenuBar(App.app.getMenuBar());
        }
        return f;
    }

    /**
     * Quit.
     */
    final void quit() {
        put(W, "" + getFrame().getSize().width);
        put(H, "" + getFrame().getSize().height);
        put(X, "" + getFrame().getLocation().x);
        put(Y, "" + getFrame().getLocation().y);
        getFrame().dispose();
        Osgw.close();
    }

    /**
     * Frame attribute key W.
     */
    private static final String W = "W";

    /**
     * Frame attribute key H.
     */
    private static final String H = "H";

    /**
     * Frame attribute key X.
     */
    private static final String X = "X";

    /**
     * Frame attribute key Y.
     */
    private static final String Y = "Y";
}
